﻿// Copyright 2009 (c) Tom van Dijck

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;

namespace Bonehead
{
    public class ProgressPanel : Panel
    {
        private int mProgressCounter = 0;
        private Pen[] mPens = new Pen[16];
        private Timer mTimer = new Timer();

        public ProgressPanel()
        {
            // Set the value of the double-buffering style bits to true.
            SetStyle(ControlStyles.DoubleBuffer |
                ControlStyles.UserPaint |
                ControlStyles.AllPaintingInWmPaint,
                true);

            UpdateStyles();

            for (int i = 0; i < 16; i++)
            {
                int C = 128 + (i * 4);
                mPens[i] = new Pen(Color.FromArgb(255, C, C, C), 3);
            }

            mTimer.Enabled = false;
            mTimer.Interval = 50;
            mTimer.Tick += new EventHandler(OnTick);
        }

        private void OnTick(object sender, EventArgs e)
        {
            mProgressCounter++;
            Invalidate();
        }

        protected override void OnVisibleChanged(EventArgs e)
        {
            mTimer.Enabled = Visible;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            for (int i = 0; i < 16; i++)
            {
                float s = (float)Math.Sin(-i * Math.PI / 8);
                float c = (float)Math.Cos(-i * Math.PI / 8);

                int idx = (mProgressCounter + i) % 16;
                g.DrawLine(mPens[idx], c * 6 + 16, s * 6 + 16, c * 15 + 16, s * 15 + 16);
            }

            string text = "Profiling.";
            int num = (mProgressCounter / 8) % 7;
            for (int i = 0; i < num; i++)
            {
                text += ".";
            }
            g.DrawString(text, SystemFonts.DefaultFont, SystemBrushes.InfoText, 35, 10);
            base.OnPaint(e);
        }
    }
}
